package cn.forge.mall.framework.core.mapper;

import cn.forge.mall.framework.common.pojo.PageParam;
import cn.forge.mall.framework.common.pojo.PageResult;
import cn.forge.mall.framework.common.pojo.SortingField;
import cn.forge.mall.framework.core.util.MybatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;

/**
 * @author liangyuanping
 * @ClassName BaseMapperX
 * @description:
 * @date 2024年03月19日
 * @version: 1.0
 */
public interface BaseMapperX<T> extends MPJBaseMapper<T> {

    default PageResult<T> selectPage(PageParam pageParam, @Param("ew") Wrapper<T> queryWrapper) {
        return selectPage(pageParam, null, queryWrapper);
    }

    default PageResult<T> selectPage(PageParam pageParam, Collection<SortingField> sortingFields, Wrapper<T> queryWrapper) {
        //Mybatis-Plus分页查询
        IPage<T> mpPage = MybatisUtils.buildPage(pageParam, sortingFields);
        selectPage(mpPage, queryWrapper);
        //返回结果
        return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
    }

    default T selectOne(SFunction<T, ?> field, Object value) {
        return selectOne(new LambdaQueryWrapper<T>().eq(field, value));
    }
}
